import numpy as np
import gzip

def load_gz(path):  #load a .npy.gz file
    if path.endswith('.gz'):
        f=gzip.open(path,'rb')
        return np.load(f)
    else:
        np.load(path)

def code2amino(code22):
    code22dic = {
        '1000000000000000000000':'A',
        '0100000000000000000000':'C',
        '0010000000000000000000':'E',
        '0001000000000000000000':'D',
        '0000100000000000000000':'G',
        '0000010000000000000000':'F',
        '0000001000000000000000':'I',
        '0000000100000000000000':'H',
        '0000000010000000000000':'K',
        '0000000001000000000000':'M',
        '0000000000100000000000':'L',
        '0000000000010000000000':'N',
        '0000000000001000000000':'Q',
        '0000000000000100000000':'P',
        '0000000000000010000000':'S',
        '0000000000000001000000':'R',
        '0000000000000000100000':'T',
        '0000000000000000010000':'W',
        '0000000000000000001000':'V',
        '0000000000000000000100':'Y',
        '0000000000000000000010':'A',  # X -> ANYONE -> A
        '0000000000000000000001':''   # J -> NULL
    }
    string = ''
    for i in code22:
        string += str(int(i))
    return(code22dic[string])

def code2label(code8):
    code8dic = {
        '100000000':'H',  # L
        '010000000':'E',  # B
        '001000000':'E',  # E
        '000100000':'H',  # G
        '000010000':'C',  # I
        '000001000':'H',  # H
        '000000100':'C',  # S
        '000000010':'C',  # T
        '000000001':''  # NULL
    }
    string = ''
    for i in code8:
        string += str(int(i))
    return(code8dic[string])

def code82code3(code8):
    if np.argmax(code8) != 8:
        if np.argmax(code8) == 3 or np.argmax(code8) == 5 or np.argmax(code8) == 0:        #code8 == [0 0 0 1 0 0 0 0 0] or code8 == [0 0 0 0 0 1 0 0 0]:
            label = [1,0,0]
        elif np.argmax(code8) == 1 or np.argmax(code8) == 2:      #code8 == [0 1 0 0 0 0 0 0 0] or code8 ==[0 0 1 0 0 0 0 0 0]:
            label = [0,1,0]
        else:
            label = [0,0,1]
        return label